package foliotracker.controllers;

import foliotracker.IPortfolioRepository;
import foliotracker.IStockPriceService;
import foliotracker.Portfolio;
import foliotracker.PortfolioCollection;
import foliotracker.PortfolioRepository;
import foliotracker.SaveLoadException;
import foliotracker.Stock;
import foliotracker.StockPriceServiceException;
import foliotracker.UnknownStockSymbolException;
import foliotracker.YahooStockPriceService;
import foliotracker.GUI.NewPortfolioDialog;
import foliotracker.views.IFolioTrackerView;

/**
 * Main controller for the application.
 */
public class FolioTrackerController
{
	private IFolioTrackerView view;
	private PortfolioCollection collection;
	private PortfolioCollectionController collectionController;
	
	public FolioTrackerController()
	{
	}
	
	public void setView(IFolioTrackerView view)
	{
		this.view = view;
	}
	
	/**
	 * Instructs the view to close all open documents and attempts to load the specified file.  If the file is loaded successfully,
	 * the view is instructed to show the overview tab for the portfolio collection; otherwise an error should be shown through the view. 
	 * @param path
	 */
	public void load(String path) 
	{
		IPortfolioRepository repository = new PortfolioRepository();
		
		try
		{
			view.closeOpenDocuments();
			collection = repository.load(path);
			collectionController = new PortfolioCollectionController(view, collection);
			
			view.openPortfolioCollectionView(collectionController, collection);	
		}
		catch (SaveLoadException ex)
		{
			view.showError(ex.getMessage());
		}
		
	}
	
	/**
	 * Saves the current file to the specified path.
	 * @param path
	 */
	public void save(String path)
	{
		IPortfolioRepository save = new PortfolioRepository();
		
		try
		{
			save.save(path, collection);
		}
		catch (SaveLoadException ex)
		{
			view.showError(ex.getMessage());
		
		}	
	}
	
	/**
	 * Instructs the view to close all open documents and then shows the overview tab for an empty portfolio collection.
	 */
	public void newFile()
	{
		view.closeOpenDocuments();
		collection = new PortfolioCollection();
		collectionController = new PortfolioCollectionController(view, collection);
		
		view.openPortfolioCollectionView(collectionController, collection);	
	}
	
	
	public void newPortfolio()
	{
		NewPortfolioDialog dialog = new NewPortfolioDialog(collectionController);
		dialog.setVisible(true);
	}
	
	/**
	 * Updates the prices of all the stock to current market values.  Updates the view with percentage complete along the way.
	 */
	public void updatePrices()
	{
		IStockPriceService service = new YahooStockPriceService();
		view.updatePercentageComplete(0);
		int i = 1;
		
		try
		{
			for (Portfolio portfolio: collection.getPortfolios())
			{
				for (Stock stock: portfolio.getStocks())
				{
					try 
					{
						stock.setPrice(service.getStockPrice(stock.getSymbol()));
					} 
				
					catch (UnknownStockSymbolException e) 
					{
						view.showError(e.getMessage());
					}	
				}
				
				portfolio.addCurrentValueToHistory();
				view.updatePercentageComplete((int)(((double)i) / collection.getPortfolios().size() * 100));
				i++;
			}
		}
		catch (StockPriceServiceException ex)
		{
			view.showError(ex.getMessage());
		}
			
		view.updatePercentageComplete(100);
	}
}
